/*
类和对象
	a.类是对象的模版，对象是类的实例化；
	b.继承是对父类的补充；
	c.通过对参数列表变化对方法重载；子类可对父类方法覆盖；
	d.使用this关键字表示本类变量或方法，使用super关键字表示父类变量或方法；
	e.可在构造方法中调用构造方法

类型转换
    向上转型
        一般定义父类变量来引用子类对象，程序动态绑定实现对子类方法调用，符合多态
    向下转型
        不确定当前变量是否为目标类对象，存在风险，
        先向上转型。再向下转型
 */
public class myClass {
    public static void main(String[] args){
        People p = new People("李四", "男" ,32);
        p.getInfo();
        System.out.println("myClass");

        //父对象指向子类的引用，才能将父对象强制转化为子对象
        People pt = new Teacher(p);
        Teacher t = (Teacher)pt;
        t.setTeachInfo("重庆商学院", "重庆");
        t.getInfo();
        t.getTeachInfo();
        System.out.println("------");

        System.out.println("向上转型测试");
        Teacher teacher = new Teacher(t);
        teacher.getTeachInfo();
        People p1 = teacher;
        System.out.println("------");
        p1.getInfo();
        System.out.println("向上转型后依旧会匹配子类型的覆盖方法");

        System.out.println("------");
        System.out.println("向下转型测试");
        //父对象指向子类的引用，才能将父对象强制转化为子对象
        People p2 = new Teacher();
        if (p2 instanceof Teacher){
            Teacher p21 = (Teacher) p2;
            System.out.println("转型成功");
        }
        else {
            System.out.println("不能转型");
        }
    }
}

class People {
    String name;
    String sex;
    int age;

    //对构造方法进行重载
    public People() {

    }

    public People(People p) {
        this.name = p.name;
        this.sex = p.sex;
        this.age = p.age;
    }

    public People(String name, String sex, int age) {
        this.name = name;
        this.sex = sex;
        this.age = age;
    }

    void setInfo(People p) {
        this.name = p.name;
        this.sex = p.sex;
        this.age = p.age;
    }

    void setInfo(String name, String sex, int age) {
        this.name = name;
        this.sex = sex;
        this.age = age;
    }

    void getInfo() {
        String info = String.format("个人信息[%s:%s:%d]", name, sex, age);
        System.out.println(info);
    }
}

class Teacher extends People{
    String school;
    String addr;

    public Teacher( People p){
        //使用SUPER调用父类的构造方法,只能调一个,语句必须位于句首.
        super(p);
        /*
        this.name = p.name;
        this.sex = p.sex;
        this.age = p.age;

         */
    }

    public Teacher() {

    }
    public Teacher(Teacher t) {
        super.setInfo(t.name, t.sex, t.age);
        setTeachInfo(t.school, t.addr);
    }

    void setTeachInfo(String school, String addr){
        this.school = school;
        this.addr = addr;
    }
    //覆盖父类的方法
    void getInfo(){
        //使用SUPER调用父类的方法
        super.getInfo();
        String info = String.format("个人信息[@%s:%s:%s:%d]",school, name, sex, age);
        System.out.println(info);
    }
    void getTeachInfo(){
        String info = String.format("职工信息[%s:%s:%s:%s:%d]",addr,school, name, sex, age);
        System.out.println(info);
    }
}